package psn.hui.sys.controller;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.News;
import psn.hui.sys.domain.User;
import psn.hui.sys.service.NewsService;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;
import psn.hui.sys.utils.WebUtils;
import psn.hui.sys.vo.NewsVo;

/**
 * 	系统公告管理控制器
 * @author hui
 *
 */
@RestController
@RequestMapping("news")
public class NewsController {
	@Autowired
	private NewsService newsService;
	
	/**
	 * 	条件查询公告列表
	 * @param newsVo
	 * @return
	 */
	@RequestMapping("newsList")
	public DataGridView newsList(NewsVo newsVo) {
		return newsService.queryAllNews(newsVo);
	}
	/**
	 * 	添加系统公告
	 * @param news
	 * @return
	 */
	@RequestMapping("addNews")
	public JsonResult addNews(News news) {
		try {
			User user = (User)WebUtils.getHttpSession().getAttribute("user");
			news.setOpername(user.getRealname());
			news.setCreatetime(new Date());
			if(newsService.addNews(news) > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}
	/**
	 * 	修改系统公告
	 * @param news
	 * @return
	 */
	@RequestMapping("updateNews")
	public JsonResult updateNews(News news) {
		try {
			if(newsService.updateNews(news) > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}
	/**
	 * 	根据id删除系统公告
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteNewsById")
	public JsonResult deleteNewsById(Integer id) {
		try {
			if(newsService.deleteNewsById(id)> 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	/**
	 * 	批量根据id删除系统公告
	 * @param id
	 * @return
	 */
	@RequestMapping("batchDeleteNewsByIds")
	public JsonResult batchDeleteNewsByIds(NewsVo newsVo) {
		try {
			if(newsService.batchDeleteNewsByIds(newsVo.getIds())> 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	
	/**
	 * 	根据id获取公告
	 * @param id
	 * @return
	 */
	@RequestMapping("loadNewsById")
	public News loadNewsById(Integer id) {
		try {
			return newsService.loadNewsById(id);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return new News();
	}
	
}
